package com.mb.api.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.api.service.IMemberService;
import com.mb.app.dao.TbMemberInfoMapper;
import com.mb.app.domain.TbMemberInfo;
import com.mb.app.task.AsyncNotifyTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 会员信息服务实现
 *
 * @author liliping
 * @create 2019-03-11 8:56 AM
 **/
@Service("tbmemberService")
@Slf4j
public class MemberServiceImpl extends ServiceImpl<TbMemberInfoMapper, TbMemberInfo> implements IMemberService {


    @Autowired
    private AsyncNotifyTask asyncNotifyTask;

    @Override
    public int submit(TbMemberInfo memberInfo) {
        int instr = 0;
        try {
            if (memberInfo != null) {
                memberInfo.setRegTime(new Date());
                // 计算总金额
                memberInfo.setTotalPrice(this.getTotalPrice(memberInfo.getSetMeal(),memberInfo.getNum()));
                instr = this.baseMapper.insert(memberInfo);
                if (instr == 1 ) {
                    asyncNotifyTask.Notify(memberInfo);
                }
            }
        } catch (Exception e) {
            log.error("submit",e);
        }
        return instr;
    }


    public BigDecimal getTotalPrice(String setMeal,Integer num) {
        BigDecimal totalPrice = new BigDecimal(0);
        if (setMeal != null && !"".equals(setMeal)) {
            if (setMeal.equals("1")) {
                if (num != null && num != 0) {
                    totalPrice = new BigDecimal(35).multiply(new BigDecimal(num));
                }
            }else if (setMeal.equals("2")) {
                if (num != null && num != 0) {
                    totalPrice = new BigDecimal(45).multiply(new BigDecimal(num));
                }
            }else if (setMeal.equals("3")) {
                if (num != null && num != 0) {
                    totalPrice = new BigDecimal(55).multiply(new BigDecimal(num));
                }
            }else if (setMeal.equals("11")) {
                totalPrice = new BigDecimal(180);
            }else if (setMeal.equals("22")) {
                totalPrice = new BigDecimal(220);
            }else if (setMeal.equals("33")) {
                totalPrice = new BigDecimal(280);
            }else if (setMeal.equals("111")) {
                totalPrice = new BigDecimal(350);
            }else if (setMeal.equals("222")) {
                totalPrice = new BigDecimal(400);
            }else if (setMeal.equals("333")) {
                totalPrice = new BigDecimal(520);
            }
        }
        return totalPrice;
    }
}
